from pathlib import Path
import random
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import NoSuchElementException
from bs4 import BeautifulSoup

from utils.tools import Tools


class Parser(Tools):

    """ Этот класс представляет парсер """

    def __init__(self, data, proxy_list=None):

        self.display_browser = data[0]
        self.link_to_page = data[2]
        self.proxy_list = proxy_list
        self.number_of_places = data[3]


    def work_with_addresses(self):

        """ Работаем с адресами """

        def enter_the_address(browser, last_page, address):

            """ Вводим адрес """

            input_field = browser.find_element(
                By.XPATH,
                '//input[@placeholder="Поиск мест и адресов"]'
            )
            self.enter_text_in_field(input_field, address)
            input_field.send_keys(Keys.ENTER)

            last_page.append(browser.current_url)


        def check_what_appeared(browser, last_page):

            """ Проверяем что появилось """

            def wait_for_a_new_page(browser, last_page):

                """ Ждем появления новой страницы """

                while True:

                    current_link = browser.current_url

                    if not current_link == last_page[-1]:

                        print('Появилась новая страница')
                        break


            # ВЫШЕ ОПРЕДЕЛЕНИЕ ФУНКЦИЙ

            wait_for_a_new_page(browser, last_page)

            what_appeared = self.waiting_for_one_thing(
                browser,
                '//span[contains(text(), "Рейтинг")]',
                '//*[contains(text(), "Что посетить")]'
            )

            return what_appeared


        def work_with_the_received_places(link_list_of_places, address, type_of_activity):

            """ Работаем с полученными местами """

            def get_data_to_add(browser, address, type_of_activity):

                """ Получаем данные со страницы """

                company_name = self.get_the_company_name(browser)
                all_numbers = self.get_all_numbers(browser)
                email_yandex_map = self.get_email_yandex_map(browser)
                link_to_the_site = self.get_links_to_website(browser)
                full_address = self.get_the_full_address_of_the_place(browser)
                list_of_mail_from_the_site = self.get_the_mail_list_from_the_website(browser, link_to_the_site)

                data = [address, type_of_activity, company_name, all_numbers, email_yandex_map,
                        link_to_the_site, list_of_mail_from_the_site, full_address]

                return data


            # ВЫШЕ ОПРЕДЛЕНИЕ ФУНКЦИЙ

            for count, link in enumerate(link_list_of_places):

                browser = self.open_the_desired_page(link)
                data = get_data_to_add(browser, address, type_of_activity)
                self.add_data_to_excel(data)

                browser.quit()

                if count == self.number_of_places: break


        # ВЫШЕ ОПРЕДЕЛЕНИЕ ФУНКЦИЙ

        last_page = []
        list_of_addresses = self.get_addresses('all')

        for address in list_of_addresses:

            browser = self.open_the_desired_page(self.link_to_page)
            address, type_of_activity = self.get_the_address_and_type_of_activity(address)

            enter_the_address(browser, last_page, address)
            what_appeared = check_what_appeared(browser, last_page)
            self.move_to_the_last_element(browser, what_appeared)
            link_list_of_places = list_of_links_to_places = self.get_a_list_of_links_to_places(browser)
            work_with_the_received_places(link_list_of_places, address, type_of_activity)

            print(f'link_list_of_places - {link_list_of_places}')
            print(f'link_list_of_places - {len(link_list_of_places)}')


    def start_parsing(self):

        """ Начать парсинг """

#        self.work_with_addresses()
#        self.bring_excel_to_the_desired_from()

#        list_of_links = 'https://mariuskaraoke.ru/' #'\nhttps://vzmoscow.ru/galleries/gallery_belyaevo/'
#        list_of_links = 'https://laura-dent.ru/'
#        list_of_links = 'https://vzmoscow.ru/galleries/gallery_belyaevo/\nhttps://mariuskaraoke.ru/'
#        list_of_links = 'https://oldboybarbershop.com/'
#        list_of_links = 'https://m-dental.ru/polozhenie_o_skidkah/'
#        list_of_links = 'http://dentaclinic.su'
#        list_of_links = 'https://vh52.timeweb.ru/blocked/?ref=ddxfitness.ru'
#
#        browser = self.open_the_desired_page('https://2ip.ru/', proxy=('random', ['176.9.119.170:3128']))
        browser = self.open_the_desired_page()
        result = self.get_the_mail_list_from_the_website(browser, list_of_links)
        print(f'result - {result}')
        input('End?')
